<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>首页</title>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <style>
        /* 页面整体样式 */
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f9f9f9;
            color: #333;
        }

        h1 {
            text-align: center;
            color: #333;
            padding: 20px;
            margin: 0;
            background-color: #4CAF50;
            color: white;
        }

        h2 {
            margin-top: 20px;
            color: #555;
            text-align: left;
        }

        ul {
            list-style-type: none;
            padding: 0;
        }

        li {
            background-color: #fff;
            margin: 10px 0;
            padding: 15px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        .lottery-form {
            display: inline-block;
            margin-top: 10px;
        }

        .lottery-button {
            background-color: #4CAF50;
            color: white;
            border: none;
            padding: 10px 20px;
            cursor: pointer;
            border-radius: 5px;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        .lottery-button:hover {
            background-color: #45a049;
            transform: scale(1.05);
        }

        .lottery-button:active {
            background-color: #3e8e41;
            transform: scale(0.98);
        }

        .result-list {
            margin-top: 20px;
            padding-left: 20px;
        }

        .result-list li {
            font-size: 14px;
            color: #333;
        }

        .result-list li:last-child {
            margin-bottom: 0;
        }

        .result-list li.error {
            color: red;
        }

        .lottery-container {
            max-width: 900px;
            margin: 20px auto;
            padding: 20px;
            background-color: #fff;
            box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
            border-radius: 10px;
        }

        .logout-link {
            display: inline-block;
            margin-right: 10px;
            padding: 8px 12px;
            font-size: 14px;
            color: #fff;
            background-color: #007bff;
            text-decoration: none;
            border-radius: 5px;
            transition: background-color 0.3s ease, transform 0.2s ease;
        }

        .logout-link:hover {
            background-color: #0056b3;
            text-decoration: none;
            transform: scale(1.05);
        }

        .logout-link:active {
            background-color: #003f7f;
            transform: scale(0.98);
        }

        .remaining-attempts {
            font-weight: bold;
            color: #d9534f;
        }
    </style>
</head>
<body>
    <h1>欢迎, {{ user.nickname }}!</h1>

    <!-- 按钮链接区 -->
    <div style="text-align: center; margin-top: 10px;">
        <a href="{% url 'users:logout' %}" class="logout-link">注销</a>
        <a href="{% url 'home:show' %}" class="logout-link">查看结果</a>
    </div>

    <!-- 抽签区域 -->
    <div class="lottery-container">
        <h2>剩余抽签次数：</h2>
        <ul>
            {% if majors1 %}
                {% for major_select in majors1 %}
                    <li>
                        {{ major_select.major.name }}专业 - {{ major_select.major.get_level_display }}组
                        - <span class="remaining-attempts" id="remaining-{{ major_select.major.id }}">{{ major_select.num }}</span> 人
                        {% if major_select.num %}
                            <form class="lottery-form" method="post" data-major-id="{{ major_select.major.id }}">
                                {% csrf_token %}
                                <button type="button" class="lottery-button">抽签</button>
                            </form>
                        {% endif %}

                        <ul class="result-list" id="result-list-{{ major_select.major.id }}">
                            {% if major_select.lottery_results %}
                                {% for result in major_select.lottery_results %}
                                    <li>{{ result }}</li>
                                {% endfor %}
                            {% endif %}
                        </ul>
                    </li>
                {% endfor %}
            {% else %}
                <p style="color: #888; text-align: center;">暂无抽签记录。</p>
            {% endif %}
        </ul>
    </div>

    <script>
        $(document).ready(function () {
            $(".lottery-button").on("click", function (event) {
                const button = $(this);
                const form = button.closest(".lottery-form");
                const majorId = form.data("major-id");
                const csrfToken = form.find("input[name=csrfmiddlewaretoken]").val();
                const resultList = $("#result-list-" + majorId);
                const remainingAttemptsSpan = $("#remaining-" + majorId);

                $.ajax({
                    url: "{% url 'home:lottery' %}",  // Replace with the actual lottery URL
                    type: "POST",
                    data: {
                        major_id: majorId,
                        user_id: "{{ user.id }}",
                        csrfmiddlewaretoken: csrfToken
                    },
                    success: function (response) {
                        if (response.res_id !== -99) {
                            // Append new result to the list
                            resultList.append("<li>抽到的编号为 " + response.res_id + "</li>");

                            // Update the remaining attempts count dynamically
                            remainingAttemptsSpan.text(response.remaining_attempts);  // Update with the remaining attempts from the server
                        } else {
                            resultList.append("<li class='error'>当前组的签已抽完</li>");
                        }
                    },
                    error: function () {
                        resultList.append("<li class='error'>发生错误，请稍后再试</li>");
                    }
                });
            });
        });
    </script>
</body>
</html>
